package org.hepeng.app;

import com.google.common.collect.Maps;
import com.google.errorprone.annotations.concurrent.LazyInit;
import lombok.AllArgsConstructor;
import org.apache.tomcat.util.codec.binary.StringUtils;
import org.hepeng.chaincode.contract.api.Result;
import org.hepeng.chaincode.contract.api.UserContract;
import org.hepeng.chaincode.contract.api.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author he peng
 * @date 2022/3/25
 */

@RestController
public class Controller {

    @Autowired
    UserContract userContract;

    @Autowired
    ApplicationContext context;

    @GetMapping("/user/{key}")
    public Map<String , Object> getUser(@PathVariable("key") String key) {

//        Object userContract1 = context.getBean(UserContract.class);
//        UserContract userContract = (UserContract) context.getBean("UserContract");
        Map<String, Object> map = Maps.newConcurrentMap();

        Result<UserInfo> result = userContract.getUser(null, key);
        map.put("payload", result);
        map.put("status", "ok");
        return map;
    }

    @PostMapping("/user-reg")
    public Map<String , Object> regUser(@RequestBody UserInfo userInfo) {
        Map<String, Object> map = Maps.newConcurrentMap();

        Result<UserInfo> result = userContract.regUser(null, userInfo);
        map.put("payload", result);
        map.put("status", "ok");
        return map;
    }
}
